#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;

pthread_mutex_t mutex;
void printer(const char *s)
{
    while (*s)
    {
        putchar(*s);
        fflush(stdout);
        s++;
        sleep(1);
    }
}
void *fun1(void *arg)
{
    pthread_mutex_lock(&mutex);
    printer((char *)"hello ");
    pthread_mutex_unlock(&mutex);
    return NULL;
}

void *fun2(void *arg)
{
    pthread_mutex_lock(&mutex);
    printer((char *)"world ");
    pthread_mutex_unlock(&mutex);
    return NULL;
}
int main()
{
    pthread_t tid1, tid2;
    int ret = -1;
    ret = pthread_mutex_init(&mutex, NULL);
    
    if (ret != 0)
    {
        printf("线程锁初始化失败\n");
        return 1;
    }
    ret = pthread_create(&tid1, NULL, fun1, NULL);
    // if (ret != 0)
    // {
    //     printf("线程1创建失败\n");
    //     return 1;
    // }
    ret = pthread_create(&tid2, NULL, fun2, NULL);
    // if (ret != 0)
    // {
    //     printf("线程2创建失败\n");
    //     return 1;
    // }


    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    pthread_mutex_destroy(&mutex);
    return 0;
}